/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "<key_word>_srv_client.h"
#include "string_ex.h"
#include "datetime_ex.h"
#include "ipc_skeleton.h"
#include "iservice_registry.h"
#include "if_system_ability_manager.h"
#include "system_ability_definition.h"
#include "<key_word>_errors.h"
#include "<key_word>_common.h"
#include "hilog_wrapper.h"

namespace <namespace> {
namespace <Subsys_name> {
<Key_word>SrvClient::<Key_word>SrvClient() {}
<Key_word>SrvClient::~<Key_word>SrvClient() {}

ErrCode <Key_word>SrvClient::Connect()
{
    std::lock_guard<std::mutex> lock(mutex_);
    if (proxy_ != nullptr) {
        return ERR_OK;
    }
    sptr<ISystemAbilityManager> sysMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
    if (sysMgr == nullptr) {
        <KEY_WORD>_HILOGE(<KEY_WORD>_SRV, "failed to GetSystemAbilityManager,%{public}d",<SERVICE_ID>);
        return E_GET_SYSTEM_ABILITY_MANAGER_FAILED;
    }
    sptr<IRemoteObject> remoteObject_ = sysMgr->CheckSystemAbility(<SERVICE_ID>);
    if (remoteObject_ == nullptr) {
        <KEY_WORD>_HILOGE(<KEY_WORD>_SRV, "failed to GetSystemAbility by %{public}d",<SERVICE_ID>);
        return E_GET_SYSTEM_ABILITY_MANAGER_FAILED;
    }

    deathRecipient_ = sptr<IRemoteObject::DeathRecipient>(new <Key_word>SrvDeathRecipient());
    if (deathRecipient_ == nullptr) {
        <KEY_WORD>_HILOGE(<KEY_WORD>_SRV, "failed to Create DeathRecipinent,%{public}d",<SERVICE_ID>);
        return ERR_NO_MEMORY;
    }
    if ((remoteObject_->IsProxyObject()) && (!remoteObject_->AddDeathRecipient(deathRecipient_))) {
        <KEY_WORD>_HILOGW(<KEY_WORD>_SRV, "Add death recipient to failed,%{public}d",<SERVICE_ID>);
        return E_ADD_DEATH_RECIPIENT_FAILED;
    }

    proxy_ = iface_cast<I<Key_word>Srv>(remoteObject_);
    return ERR_OK;
}

void <Key_word>SrvClient::ResetProxy(const wptr<IRemoteObject>& remote)
{
    std::lock_guard<std::mutex> lock(mutex_);
    RETURN_IF(proxy_ == nullptr);
    auto serviceRemote = proxy_->AsObject();
    if ((serviceRemote != nullptr) && (serviceRemote == remote.promote())) {
        serviceRemote->RemoveDeathRecipient(deathRecipient_);
        proxy_ = nullptr;
    }
}

void <Key_word>SrvClient::<Key_word>SrvDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& remote)
{
    if (remote == nullptr) {
        return;
    }
    <Key_word>SrvClient::GetInstance().ResetProxy(remote);
}
<declare>

}  // namespace <Subsys_name>
}  // namespace <namespace>
